package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.ArticleDataDao;
import cc.linker.steplink.crm.dao.CustomerDao;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import cc.linker.steplink.crm.response.ResponseException;
import cc.linker.steplink.crm.service.ArticleDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleDataServiceImpl implements ArticleDataService {

    @Autowired
    private ArticleDataDao articleDataDao;
    @Autowired
    private CustomerDao customerDao;


    @Override
    public PageInfo getLogByCustomerId(String customerId, Integer pn) throws GlobalException {
        if (pn != null && pn <= 0) {
            pn = 1;
        } else if (pn == null) {
            pn = 1;
        }
//        if (StringUtils.isEmpty(openId) || StringUtils.isEmpty(token)) {
//            throw new GlobalException(ResponseCode.ID_NULL);
//        }
//        String customerId = customerDao.selectCustomerIdByOpenIdAndToken(openId, token);
        if (StringUtils.isEmpty(customerId)) {
            throw new GlobalException(new ResponseException(400, "信息获取失败"));
        }
        PageHelper.startPage(pn, 20);
        return new PageInfo<>(articleDataDao.getLogById(customerId));
    }

    @Override
    public PageInfo listData(Integer pn, Integer pageSize, String orgId) {
        if (pn != null && pn <= 0) {
            pn = 1;
        } else if (pn == null) {
            pn = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 20;
        }
        PageHelper.startPage(pn, pageSize);
        return new PageInfo<>(articleDataDao.listData(orgId));
    }

    @Override
    public Object getDataByCustomerId(String customerId) {
        return articleDataDao.getDataById(customerId);
    }

    @Override
    public Object getLogByCustomerId(String customerId, Integer pn, String start, String end) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)) {
            map.put("start", start);
            map.put("end", end);
        }
        if (pn != null && pn <= 0) {
            pn = 1;
        } else if (pn == null) {
            pn = 1;
        }
        PageHelper.startPage(pn, 20);
        map.put("customerId", customerId);
        return new PageInfo<>(articleDataDao.getLogByCustomerId(map));
    }
}
